//
//  LYRequest.m
//  LYDevelopKit
//
//  Created by 李阳 on 2018/6/1.
//

#import "LYRequest.h"
#import "YTKNetworkConfig.h"

@interface LYRequest ()

@property (nonatomic, strong) NSArray<NSString *>*authorizationHeaders;
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *headerFields;

@end
@implementation LYRequest

- (instancetype)init
{
    self = [super init];
    if (!self) return nil;
    _timeoutInterval = 60;
    _method = YTKRequestMethodGET;
    _requestSerializerType = -1;
    _responseSerializerType = -1;
    _allowsCellularAccess = YES;
    _cacheTimeInSeconds = -1;
    _cacheVersion = 0;
    return self;
}


#pragma mark - Override

- (NSString *)requestUrl
{
    return _urlPath.length ? _urlPath : @"";
}
- (NSTimeInterval)requestTimeoutInterval
{
    return _timeoutInterval;
}
- (id)requestArgument
{
    return _argument;
}
- (YTKRequestMethod)requestMethod
{
    return _method;
}
- (YTKRequestSerializerType)requestSerializerType
{
    return _requestSerializerType;
}
- (YTKResponseSerializerType)responseSerializerType
{
    return _responseSerializerType;
}
- (NSArray<NSString *> *)requestAuthorizationHeaderFieldArray
{
    return _authorizationHeaders;
}
- (NSDictionary<NSString *,NSString *> *)requestHeaderFieldValueDictionary
{
    return _headerFields;
}
- (BOOL)allowsCellularAccess
{
    return _allowsCellularAccess;
}
- (id)jsonValidator
{
    return _jsonValidator;
}
- (BOOL)statusCodeValidator
{
    if (!self.error)
    {
        return [super responseStatusCode];
    }
    
    NSInteger statusCode = self.error.code;
    if (statusCode == [YTKNetworkConfig sharedConfig].defaultSuccessCode)
    {
        return YES;
    }
    if (_successCodeSet.count)
    {
        return [_successCodeSet containsObject:@(statusCode)];
    }
    
    if (_errorCodeSet.count)
    {
        return ![_errorCodeSet containsObject:@(statusCode)];
    }
    
    return NO;
}

- (NSInteger)cacheTimeInSeconds
{
    return _cacheTimeInSeconds;
}
- (long long)cacheVersion
{
    return _cacheVersion;
}

- (void)requestCompletePreprocessor
{
    [super requestCompletePreprocessor];
    !_completePreprocessor ?: _completePreprocessor(self);
}
- (void)requestCompleteFilter
{
    [super requestCompleteFilter];
    !_completeFilter ?: _completeFilter(self);
}
- (void)requestFailedPreprocessor
{
    [super requestFailedPreprocessor];
    !_failedPreprocessor ?: _failedPreprocessor(self);
}
- (void)requestFailedFilter
{
    [super requestFailedFilter];
    !_failedFilter ?: _failedFilter(self);
}

- (void)addValue:(NSString *)value forHeaderField:(NSString *)field
{
    NSParameterAssert(value.length);
    NSParameterAssert(field.length);
    self.headerFields[field] = value;
}
- (void)setAuthorizationWithUsername:(NSString *)username password:(NSString *)password
{
    NSParameterAssert(username.length);
    NSParameterAssert(password.length);
    _authorizationHeaders = @[username, password];
}


#pragma mark - Getter

- (NSMutableDictionary<NSString *,NSString *> *)headerFields
{
    if (_headerFields) return _headerFields;
    _headerFields = [NSMutableDictionary dictionary];
    return _headerFields;
}


@end
